

function isIntegerInRange (s, a, b)
{
      //var num = parseInt (s);
      num =s;
      return ((num >= a) && (num <= b));
}


function isYear (s)
{
	if (!isInteger(s)) return false;
	return (s.length == 4);
}

function isMonth (s)
{
    if (!isInteger(s)) return false;
    return isIntegerInRange (s, 1, 12);
}

function isDay (s)
{
    if (!isInteger(s)) return false;
    return isIntegerInRange (s, 1, 31);
}

// Attempting to make this library run on Navigator 2.0,
// so I'm supplying this array creation routine as per
// JavaScript 1.0 documentation.

function makeArray(n) {
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   }
   return this
}

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;


function daysInFebruary (year)
{
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}


function isDateValid (month,day,year)
{

    if (! (isYear(year) && isMonth(month) && isDay(day)))
	   return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year);
    var intMonth = parseInt(month);

    var intDay = parseInt(day);


    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false;

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear)))
    	return false;

    return true;
}


function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim



function isDateBefore(selected_month,selected_date,selected_year)
{

	DateErrorMessage = "";
	var invalid_date_day = 0;
	var invalid_date_before = 0;

	var d = new Date();
	var current_month = d.getMonth() + 1;
	var current_date = d.getDate();

	var current_year = d.getFullYear();

	if (selected_year < current_year && !invalid_date_before)
	{
		invalid_date_before = 1;
		return false;
	}
	if (selected_year == current_year && !invalid_date_before)
	{
		if (selected_month < current_month)
		{
			invalid_date_before = 1;
			return false;
		}
		if (selected_month == current_month)
		{
			if (selected_date < current_date)
			{
				invalid_date_before = 1;
				return false;
			}
		}
	}
	return true;
}


function isDateAfter(selected_month,selected_date,selected_year)
{

	DateErrorMessage = "";
	var invalid_date_day = 0;
	var invalid_date_after = 0;

	var d = new Date();
	var current_month = d.getMonth() + 1;
	var current_date = d.getDate();

	var current_year = d.getFullYear();

	if (selected_year > current_year && !invalid_date_after)
	{
		invalid_date_after = 1;
		return false;
	}
	if (selected_year == current_year && !invalid_date_after)
	{
		if (selected_month > current_month)
		{
			invalid_date_after = 1;
			return false;
		}
		if (selected_month == current_month)
		{
			if (selected_date >= current_date)
			{
				invalid_date_after = 1;
				return false;
			}
		}
	}
	return true;
}

// isDateOut by Raj Vallamsetla.. Function checks whether DateIn is before DateOut hahaaa


function isDateOut(dateIn_month,dateIn_date,dateIn_year,dateOut_month,dateOut_date,dateOut_year)
{

	if(dateIn_date.length==1)
	dateIn_date = '0'+dateIn_date;

	if(dateOut_date.length==1)
	dateOut_date = '0'+dateOut_date;

	DateErrorMessage = "";
	var invalid_date_day = 0;
	var invalid_date_before = 0;

	var d = new Date();
	//var current_month = d.getMonth() + 1;
	//var current_date = d.getDate();

	//var current_year = d.getFullYear();

	if (dateOut_year < dateIn_year && !invalid_date_before)
	{
		invalid_date_before = 1;
		return false;
	}
	if (dateOut_year == dateIn_year && !invalid_date_before)
	{
		if (dateOut_month < dateIn_month)
		{
			invalid_date_before = 1;
			return false;
		}
		if (dateOut_month == dateIn_month)
		{
			if (dateOut_date < dateIn_date)
			{
				invalid_date_before = 1;
				return false;
			}
		}
	}
	return true;
}




// Returns true if character c is a digit (0 .. 9).
function isDigit (c)
{
	return ((c >= "0") && (c <= "9"))
}

// Returns true if character c is an English letter (A .. Z, a..z).
function isLetter (c)
{
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isInteger (s)
{

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (var i = 0; i < s.length; i++)
    {
	// Check that current character is number.
	var c = s.charAt(i);

	if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isCharcters(s)
{
   // Search through string's characters one by one
    // until we find a numeric character.
    // When we do, return false; if we don't, return true.

    for (var i = 0; i < s.length; i++)
    {
	// Check that current character is number.
	var c = s.charAt(i);

	if (!isLetter(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isCharacters(s)
{

    for (var i = 0; i < s.length; i++)
    {
	// Check that current character is number.
	var c = s.charAt(i);

	if (!( (isLetter(c)) || (isDigit(c)) || isSpecialLetter(c) ))
	return false;
    }

    return true;
}



//Function to check whether the given String is Float by Raj Vallamsetla

function isFloat(s)

{
	var i;
    var seenDecimalPoint = false;
	var decimalPointDelimiter = ".";
 if (s == decimalPointDelimiter) return false;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}



function validateDate(temp) {
    if(temp == 'ASAP') {
        return true;
    }
    if(temp.lastIndexOf("/") == -1) {
        //errors = "Please enter a valid date, date must be in mm/dd/yyyy";
		  errors = "<li> Requested Dock Date should be in 'mm/dd/yyyy' format";
             return false;
    }

    var dateValue = temp.split("/");
    if(!isDateValid(dateValue[0],dateValue[1],dateValue[2])) {
            // errors = "Please enter a valid date, date must be in mm/dd/yyyy";
			  errors = "<li> Requested Dock Date should be in 'mm/dd/yyyy' format";
             return false;
    } else {
             //if(!isDateBefore(dateValue[0],dateValue[1],dateValue[2])) {
             //    errors = "Requested date cannot be a past date";
             //    return false;
             //}
    }
    return true;
}

function validateBookingDate(temp) {
    if(temp == 'ASAP') {
        return true;
    }
    if(temp.lastIndexOf("/") == -1) {
        //errors = "Please enter a valid date, date must be in mm/dd/yyyy";
		  errors = "<li> Booking Date  should be in 'mm/dd/yyyy' format";
             return false;
    }

    var dateValue = temp.split("/");

    // the following condition is added by Murali Krishna on 27 Jan 2005, checks if 2 '/' are entered
    if( dateValue.length == 3 )
    {
        if(!isDateValid(dateValue[0],dateValue[1],dateValue[2])) {
                // errors = "Please enter a valid date, date must be in mm/dd/yyyy";
                  errors = "<li> Booking Date should be in 'mm/dd/yyyy' format";
                 return false;
        } else {
                 //if(!isDateBefore(dateValue[0],dateValue[1],dateValue[2])) {
                 //    errors = "Requested date cannot be a past date";
                 //    return false;
                 //}
        }
     }
     else
     {
         errors = "<li> Booking Date should be in 'mm/dd/yyyy' format";
         return false;
     }
    return true;
}


function validateDateOut(dateIn,dateOut){
var errors = '';
var dateInValue		= dateIn.split("/");
var dateOutValue	= dateOut.split("/");

if(!isDateOut(dateInValue[0],dateInValue[1],dateInValue[2],dateOutValue[0],dateOutValue[1],dateOutValue[2])) {
            errors = "DateIn is greater than Date Out.Please enter valid date.";
			return false;
    }
return true;

}





function validateDateOnly(temp) {

	var errors = '';
	if(temp.lastIndexOf("/")==-1){
		errors = "Please enter a valid date, date must be in mm/dd/yyyy";
		return false;
	}


    var dateValue = temp.split("/");
    if(!isDateValid(dateValue[0],dateValue[1],dateValue[2])) {
             errors = "Please enter a valid date, date must be in mm/dd/yyyy";
			return false;
    }

    return true;
}
//commented and added by Varma (13-jun -2004)
// This function checks for valid emailId
/*function IsEmailValid(Temp)
{
	var errors = "";
	var AtSym    = Temp.value.indexOf('@');
	var Period   = Temp.value.lastIndexOf('.');
	var Space    = Temp.value.indexOf(' ');
	var Length   = Temp.value.length - 2;   // Array is from 0 to length-2

	if (((AtSym < 1) ||                     // '@' cannot be in first position
	    (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
	    (Period == Length ) ||             // Must be atleast two valid char after '.'
	    (Space  != -1)) && (Temp.value != ''))                    // No empty spaces permitted
	    {
               errors = "Please enter a valid email address";

	    }

        var pa=document.getElementById("errors");
        pa.innerHTML = errors;


}*/
function IsEmailValid(temp)
 {
    var email = temp.value;
    ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

    for(i=0; i < email.length ;i++)
    {
    if(ok.indexOf(email.charAt(i))<0){
    return (false);
    }
    }

    if (document.images)
    {
    re = /(@.*@)|(\.\.)|(\.\@)|(\_\@)|(\-\@)|(\@\-)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (!email.match(re) && email.match(re_two)) {
        return (-1);
        }

    }

 }

 function isValidEMailId(temp)
 {
    var email = temp.value;
    ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

    for(i=0; i < email.length ;i++)
    {
    if(ok.indexOf(email.charAt(i))<0){
    return (false);
    }
    }

    if (document.images)
    {
    re = /(@.*@)|(\.\.)|(\.\@)|(\_\@)|(\-\@)|(\@\-)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (!email.match(re) && email.match(re_two)) {
        return (-1);
        }

    }

 }//till here added by varma
//commented and added by varma (13-jun-2004) to check the Email valid or not
 //Added by Sarada to check emailId valid or not
/*function IsValidEmail(Temp)
{
	var errors = "";
	var AtSym    = Temp.value.indexOf('@');
	var Period   = Temp.value.lastIndexOf('.');
	var Space    = Temp.value.indexOf(' ');
	var Length   = Temp.value.length - 2;   // Array is from 0 to length-2

	if (((AtSym < 1) ||                     // '@' cannot be in first position
	    (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
	    (Period == Length ) ||             // Must be atleast two valid char after '.'
	    (Space  != -1)) && (Temp.value != ''))                    // No empty spaces permitted
	    {
               errors = "Please enter a valid email address";
               return false;
	    }

        //var pa=document.getElementById("errors");
        //pa.innerHTML = errors;

        return true;
} */

//added by varma (9-jun-2004) to restrict the consecute dots(...) in Email
function IsValidEmail(temp)
 {
    var email = temp.value;
    ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

    for(i=0; i < email.length ;i++)
    {
    if(ok.indexOf(email.charAt(i))<0){
    return (false);
    }
    }

    if (document.images)
    {
    re = /(@.*@)|(\.\.)|(\.\@)|(\_\@)|(\-\@)|(\@\-)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (!email.match(re) && email.match(re_two)) {
        return (-1);
        }

    }

 }//till here added by varma

function isValidInteger(temp)
{
    var errors = '';
    if(!isCharacters(temp.value))
        errors = 'Please enter a valid integer';

    var pa=document.getElementById("errors");
    pa.innerHTML = errors;
}


function isValidState(temp)
{
    var errors = '';
    if(!isCharcters(temp.value))
    {
        errors = 'Please enter a valid state(only letters)';
    }

    var pa=document.getElementById("errors");
    pa.innerHTML = errors;
}

function isValidCountry(temp)
{
    var errors = '';
    if(!isCharcters(temp.value))
    {
        errors = 'Please enter a valid country(only letters)';
    }

    var pa=document.getElementById("errors");
    pa.innerHTML = errors;
}

function isValidZipCode(temp)
{
    var errors = '';
    if(!isInteger(temp.value))
    {
        errors = 'Please enter a valid zip code';
    }
    var pa=document.getElementById("errors");
    pa.innerHTML = errors;
}




// This function removes the Phone Delimiters if user enters like "()" & "-"
// and NumberDelimeters like "$" & ","
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
function stripCharsInBag (s, bag)
{
	var i;
	var returnString = "";


	for (i = 0; i < s.length; i++)
	{
	   // Check that current character isn't whitespace.
	   var c = s.charAt(i);
	   if (bag.indexOf(c) == -1) returnString += c;
  	}

	return returnString;
}



 function isSpecialLetter (c)
 {
   return ( (c == ",") || (c == ".") || (c == " ") || (c == "&") || (c == "-") )
 }



function isNumericals(s)
{

    for (var i = 0; i < s.length; i++)
    {
	// Check that current character is number.
	var c = s.charAt(i);

	if (!( (isDigit(c)) || (c == "-" ) ))
	return false;
    }

    return true;
}


function sp_letter(c)
{
   return (  (c == "_") || (c == "-") )
 }

//Added by Sarada on 9th June to do validaitons for SalesOrderNo, CudtomerPo, Shipping Account

function is_Sp_Characters(s)
{

     for (var i = 0; i < s.length; i++)
       {
   	// Check that current character is number.
   	var c = s.charAt(i);

   	if (! ( isLetter(c) || isDigit(c) || sp_letter(c) ) )

   	return false;
       }

    return true;

 }


//To check whether the first character is a special character or not
//for SalesOrderNo, CudtomerPo, Shipping Account fields
//if it is special char return  true

	function check_char_position(s)
	{
		 var c = s.charAt(0);
		// alert(c);
		 if ( (c == "-")  || (c == "_") )
              	 {
         		//alert("as ");
        		return true ;
		 }
		return false ;
	}


//To do validaitons for Contact Person, Sales Person, End User, Regional Rep

 function is_Sp_Character(s)
 {

      for (var i = 0; i < s.length; i++)
        {
    	// Check that current character is number.
    	var c = s.charAt(i);

    	if (! ( isLetter(c) || isDigit(c) || sp_letter(c) || sp_chars1(c) ) )

    	return false;
        }

     return true;

 }


//To check whether the first character is a special character or not
//for Contact Person, Sales Person, End User, Regional Rep fields
//if it is special char return  true


   function check_char_position1(s)
    {
	 var c = s.charAt(0);
	 //alert(c);
	 if ( (c == ".")  || (c == ",") || (c == " ") || (c == "-")  || (c == "_") )
	 {
		//alert("as1 ");
		return true ;
	}
	return false ;
    }



 //To do validaitons for Pay Terms, Delivery Terms


  function is_Sp_Characters_Terms(s)
   {

        for (var i = 0; i < s.length; i++)
          {
      	// Check that current character is number.
      	var c = s.charAt(i);

      	if (! ( isLetter(c) || isDigit(c) || sp_chars(c) || sp_chars1(c)) )

      	return false;
          }

       return true;

    }


//To check whether the first character is a special character or not
//for Pay Terms, Delivery Terms
//if it is special char return  true


   function check_char_position2(s)
    {
	 var c = s.charAt(0);
	 //alert(c);
	 if ( (c == ".")  || (c == ",") || (c == " ") || (c == "-")  || (c == "_") ||(c == "#") || (c == "$") || (c == "&") )
	 {
		//alert("as2 ");
		return true ;
	}
	return false ;
    }


//To do validaitons for Phone No, Fax No

   function is_Sp_Characters_phone(s)
    {
     for (var i = 0; i < s.length; i++)
	{

		var c = s.charAt(i);

		if (! ( isLetter(c) || isDigit(c) || sp_chars_phone(c) ) )

		  return false;
	}
     return true;

    }


  //To check whether the first character is a special character or not
//for Phone No, Fax No
//if it is special char return  true


   function check_char_position3(s)
    {
	 var c = s.charAt(0);
	// alert(c);
	 if (  (c == "-") )
	 {
		//alert("as3");
		return true ;
	}
	return false ;
    }



  //To do validaitons for Frieght , Misc ,New Sales Tax

      function is_Sp_Characters_frieght(s)
       {
        for (var i = 0; i < s.length; i++)
   	{

   		var c = s.charAt(i);

   		if (! (  isDigit(c) || (c == ".") ) )

   		  return false;
   	}
        return true;

       }


 //To check whether the first character is a special character or not
  //for Frieght , Misc
  //if it is special char return  true

  	function check_char_position4(s)
  	{
  		 var c = s.charAt(0);
  		 if ( (c == "-")  )
               {
           		return true ;
  		 }
  		return false ;
	}





//To do validaitons for New SalesState

  function is_Sp_Characters_State(s)
    {
     for (var i = 0; i < s.length; i++)
	{

		var c = s.charAt(i);

		if (! ( isLetter(c) || sp_letter(c) || (c == " ") ) )

		  return false;
	}
     return true;

    }

//To check whether the first character is a special character or not
  //for Frieght , Misc
  //if it is special char return  true

  	function check_char_position5(s)
  	{
  		 var c = s.charAt(0);
  		// alert(c);
  		 if ( (c == "-") || (c == "_") )
                {
           		return true ;
  		 }
  		return false ;
	}




	function sp_chars(c)
	{
          return ( (c == "#") || (c == "$") || (c == "&") )
	}

	function sp_chars1(c)
	{
          return (  sp_chars(c) || (c == " ") || (c == ".") || (c == ",") )
	}



	function sp_chars_phone(c)
	{
	  return ( (c == "-") || (c == "(") || (c == ")") )
	}


//To do validaitons for Comments and Shipping Instructions

   function text_area(s)
   {
      //alert("length  "+s.length);

      if( s.length > 300)
      {
        return true;

      }

      return false;
  }


 function chkStateNames(input,control,compulsory)
 {
 	var Cont_Value = input;
 	var Cont_Name =control;
 	var Cont_Validate=compulsory;
 	//var flag=false;
 	var spcount=0;

 	for(i=0;i<Cont_Value.length;i++)
 	{
 	  var str=Cont_Value.charAt(i)
 	  if (str==" ") spcount++;
  	  if (!( (str >= "A" && str <= "Z" ) || (str >= "a" && str <= "z") || (str == "_") || (str == "-") || (str == " ") || (str >= "0" && str <= "9") ))
 	  {
 	    errMsg=  " Enter Valid State Names in "+ Cont_Name;
    	    return false;
 	  }
 	}
 	if ((spcount==i) && (i != 0))
 	//flag=false;
        return false;
 }

/** Added by Sarada on 24th Sep 2003
  * To add validations For Sales Order No
  */
function chkSalesOrderNo(value)
 {
	 var i;
 	for(i=0;i<value.length;i++)
 	{
 	  var str=value.charAt(i);
	  if (!( (str >= "A" && str <= "Z" ) || (str >= "a" && str <= "z") || (str >= "0" && str <= "9") || (str == "_") || (str == "-") || (str == "&")  || (str == "#") || (str == "$")  || (str == "(") ||(str == ")") ||(str == " ") ))
 	  {
 	   	    return false;
 	  }
 	}

   return true;
 }
/* To check starting character of sales order No */
function chkSalesOrderNoBegin(value)
 {
	 var i;

 	  var str=value.charAt(0);
	  if ( (str == "_") || (str == "-") || (str == "&") || (str == "\\") || (str == "\/") || (str == "#") || (str == "$") || (str == "*") || (str == "(") ||(str == ")") ||(str == " ") )
	  {
		  return true;
	  }

   return false;
 }


/* function chkShipMethod(s)
 {

	for (var i = 0; i < s.length; i++)
    {
		
	// Check that current character is number.
	var c = s.charAt(i);
	alert(c);

	if (! (isLetter(c)) || (isDigit(c)) )
	return false;
    }

    return true;

 }*/

 function chkShipMethod(value)
 {
	 var i;
 	for(i=0;i<value.length;i++)
 	{
 	  var str=value.charAt(i);
	  if (!( (str >= "A" && str <= "Z" ) || (str >= "a" && str <= "z") || (str >= "0" && str <= "9")))
 	  {
 	   	    return false;
 	  }
 	}

   return true;
 }

 /*
 Bhaskar added this function on 10 Feb 2004 inorder to reformat the date back to mm/dd/yyyy in case the user
 enters the same as m/d/yyyy format. 
 */
 function formatDateAsMMDDYYYY(dateVal)
 {
	if ( dateVal.length == 10 )
	{
		return dateVal;
	}
	else
	 {
		var dateValArr = dateVal.split("/");
		if ( dateValArr[0].length < 2 )
		{
			dateValArr[0] = '0'+dateValArr[0];
		}
		if ( dateValArr[1].length < 2 )
		{
			dateValArr[1] = '0'+dateValArr[1];
		}
		dateVal = dateValArr[0]+"/"+dateValArr[1]+"/"+dateValArr[2];
		return dateVal;
	 }
 }
 function noenter( arg )
 {
    var keyCode = arg.keyCode ? arg.keyCode : arg.which ? arg.which : arg.charCode;
    return !( keyCode == 13 )
 }

